"""
Models for representing subversion repositories.
"""

import datetime
import posixpath
import mimetypes
import hashlib
import time

from django.db import models
from django.core import exceptions
from django.utils import functional, safestring
from django.utils.translation import ugettext_lazy as _


class Repository(models.Model):
    """
    Model for a subversion repository.
    """
    uuid = models.CharField(max_length=128, editable=False)
    label = models.CharField(
        help_text=_('Unique label for the repository for use in the URL'),
        max_length=128, db_index=True, unique=True)
    root = models.CharField(
        help_text=_('Example: svn://example.com or file:///svn/'),
        max_length=512)
    uri = models.CharField(
        help_text=_('Externally facing URI for the repository, if available'),
        max_length=512, blank=True)
    is_private = models.BooleanField(
        help_text=_('If set as true, visitors will not be able to see this repository.'),
        default=False
        )
    force_accept_certificate = models.BooleanField(default=False)

    username = models.CharField(max_length=512, blank=True)
    password = models.CharField(max_length=512, blank=True)

    last_synced = models.DateTimeField(
        default=functional.curry(datetime.datetime.fromtimestamp, 0),
        editable=False)

    class Meta:
        #correct plural form
        verbose_name_plural = _('repositories')
        ordering = ('label',)

    def __unicode__(self):
        #equivalent to toString method in Java.
        return u'%s %s' % (self.label, self.root)

class Node(models.Model):
    '''
    Model for A node represents either a folder or a file. 
    '''
    hashable = hashlib.sha1()
    hashable.update(str(time.time()))

    path = models.CharField(max_length=1000, editable=False)
    filename = models.CharField(max_length=500, editable=False)
    repoid = models.IntegerField()
    timehash = models.CharField(max_length=20, default=hashable.hexdigest()[:10])
    kind = models.CharField(max_length=4)

    def __unicode__(self):
        return u'%s %s %s' % (self.path, self.filename, self.timehash)
